/**
 * Created by doubleyong on 2020/4/14.
 */
import router from "./index";
import store from "./../store";
function createRouterDiGui(arr) {
  const subRoutes = [];
  arr.forEach((item) => {
    if (item.componentPath === "") {
      subRoutes.push({
        path: item.menuUrl,
        name: item.pathName,
        meta: { requireAuth: true, menuId: item.menuId },
      });
    } else {
      subRoutes.push({
        path: item.menuUrl,
        name: item.pathName,
        meta: { requireAuth: true, menuId: item.menuId ,componentPath:item.componentPath},
        component: () => import("@/views/" + item.componentPath),
      });
    }
    // 判断是否有子数组
    if (item.menuChilds.length && item.menuChilds.length > 0) {
      subRoutes.push(...createRouterDiGui(item.menuChilds));
    }
  });
  return subRoutes;
}

// 执行动态添加路由
function DynamicAddRouter() {
  let subRoutes = [];
  subRoutes = createRouterDiGui(store.getters.getMenuInfo);
  console.log("路由动态");
  // console.log(subRoutes);
  router.options.routes[2].children = [...subRoutes];
  router.options.routes[2].children.push({
    path: "*",
    name: "t404",
    component: () => import(/* webpackChunkName: "t404" */"@/components/Page404"),
  });
  router.$addRoutes(router.options.routes);
  // console.log(router.options.routes);
}
export default {
  DynamicAddRouter,
};
